package gui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class CustomJPanel extends JPanel{
	
	private static final long serialVersionUID = 6302467919989665522L;
	private JFrame parent;
	private int cornerIndex;
	private Point sp;
	private Point p;
	private int	oldHeight;
	private int oldWidth;
	private boolean shouldMoveX;
	private boolean shouldMoveY;
	private int newHeight;
	private int newWidth;
	private int newLocX;
	private int newLocY;
	protected boolean isResizing;
	
	private int minofWidth = 200;
	private int minofHeight = 150;
	
	public CustomJPanel(JFrame param, int cornerIndex) {
		this.parent = param;
    	this.cornerIndex = cornerIndex;
    	setOpaque(false);
    	addMouseListener(new CustomMouseAdapter());
    	addMouseMotionListener(new CustomMouseMotionAdapter());
    	isResizing = false;
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g2d = null;
		if(isResizing){
			g.setColor(new Color(35, 35, 35));
	        g.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), cornerIndex, cornerIndex);
	        g.setColor(new Color(240, 240, 240));
			g.clearRect(0, 0+30, this.getWidth(), this.getHeight()-30);
	        g.fillRect(0, 0+30, this.getWidth(), this.getHeight()-30);
		}else{
			 g2d = (Graphics2D) g;
			g2d.setColor(new Color(35, 35, 35));
			g2d.setRenderingHint(
				    RenderingHints.KEY_ANTIALIASING,
				    RenderingHints.VALUE_ANTIALIAS_ON);
			g2d.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), cornerIndex, cornerIndex);
			g2d.setColor(new Color(240, 240, 240));
	        g2d.clearRect(0, 0+30, this.getWidth(), this.getHeight()-30);
	        g2d.fillRect(0, 0+30, this.getWidth(), this.getHeight()-30);
		}
		
		if(g2d == null){
			g2d = (Graphics2D) g;
		}
		g2d.setColor(new Color(100, 100, 100));
        g2d.drawRect(0, 0+30, this.getWidth()-1, this.getHeight()-30-1);
        
		g2d.setColor(new Color(60, 60, 60));
		g2d.fillRect(0, 30, this.getWidth(), 100);
		float thickness = 4;
		g2d.setStroke(new BasicStroke(thickness));
		g2d.setColor(new Color(50, 50, 50));
		g2d.drawLine(0, 130, this.getWidth(), 130);
		g2d.setRenderingHint(
			    RenderingHints.KEY_ANTIALIASING,
			    RenderingHints.VALUE_ANTIALIAS_DEFAULT);
		thickness = 1;
		g2d.setStroke(new BasicStroke(thickness));
		
		
		
	}
	
	class CustomMouseAdapter extends MouseAdapter{
		@Override
		public void mouseExited(MouseEvent arg0) {
			if (sp == null){
				setCursor( Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR)); 
			}
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			sp = arg0.getPoint(); 
			oldHeight = getSize().height;
			oldWidth = getSize().width;
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			sp = null;
			isResizing = false;
			repaint();
		}		
		
	}
	
	class CustomMouseMotionAdapter extends MouseMotionAdapter{
		
		@Override
		public void mouseDragged(MouseEvent arg0) {
			isResizing = true;
			p = arg0.getPoint();
        	newHeight = getSize().height;
        	newWidth = getSize().width;
        	newLocX = parent.getLocation().x;
        	newLocY = parent.getLocation().y;
			//resize events
			int cursorType = getCursor().getType();
			if (cursorType == Cursor.SE_RESIZE_CURSOR){
				newHeight = oldHeight + p.y - sp.y;
				newWidth = oldWidth + p.x - sp.x;
			}else if(cursorType== Cursor.S_RESIZE_CURSOR){
				newHeight = oldHeight + p.y - sp.y;
			}else if(cursorType== Cursor.E_RESIZE_CURSOR){
				newWidth = oldWidth + p.x - sp.x;
			}else if(cursorType== Cursor.NW_RESIZE_CURSOR){
				newHeight = oldHeight - (p.y - sp.y);
				newWidth = oldWidth - (p.x - sp.x);
				newLocX += (p.x - sp.x);
				newLocY += (p.y - sp.y);
				oldWidth = newWidth;
				oldHeight = newHeight;
			}else if(cursorType== Cursor.N_RESIZE_CURSOR){
				newHeight = oldHeight - (p.y - sp.y);
				newLocY += (p.y - sp.y);
				oldHeight = newHeight;
			}else if(cursorType== Cursor.W_RESIZE_CURSOR){
				newWidth = oldWidth - (p.x - sp.x);
				newLocX += (p.x - sp.x);
				oldWidth = newWidth;
			}else if(cursorType== Cursor.NE_RESIZE_CURSOR){
				newHeight = oldHeight - (p.y - sp.y);
				newWidth = oldWidth + (p.x - sp.x);
				newLocY += (p.y - sp.y);
				oldHeight = newHeight;
			}else if(cursorType== Cursor.SW_RESIZE_CURSOR){
				newHeight = oldHeight + (p.y - sp.y);
				newWidth = oldWidth - (p.x - sp.x);
				newLocX += (p.x - sp.x);
				oldWidth = newWidth;
			}else if(cursorType == Cursor.HAND_CURSOR){
				newLocX += (p.x - sp.x);
				newLocY += (p.y - sp.y);
			}
			
			if(newWidth<minofWidth){
				newWidth = minofWidth;
				oldWidth = minofWidth;
				shouldMoveX = false;
				setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
			}else{
				shouldMoveX = true;
			}
			
			if(newHeight < minofHeight){
				newHeight = minofHeight;
				oldHeight = minofHeight;
				shouldMoveY = false;
				setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
			}else{
				shouldMoveY = true;
			}
			if(shouldMoveX&&shouldMoveY){
				parent.setLocation(newLocX, newLocY);
				parent.setSize(newWidth,newHeight);
			}else if(shouldMoveX){
				parent.setLocation(newLocX, parent.getLocation().y);
				parent.setSize(newWidth, parent.getSize().height);
			}else if(shouldMoveY){
				parent.setLocation(parent.getLocation().x, newLocY);
				parent.setSize(parent.getSize().width, newHeight);
			}else{
				parent.setLocation(parent.getLocation().x, parent.getLocation().y);
			}
		}

		@Override
		public void mouseMoved(MouseEvent arg0) {
			Point p = arg0.getPoint(); 
			int tempw = getSize().width;
			int temph = getSize().height;
			if (p.x>tempw-8 && p.y>temph-8){
				setCursor(Cursor.getPredefinedCursor(Cursor.SE_RESIZE_CURSOR)); 
			}else if(p.x>tempw-5){
				setCursor(Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR));
			}else if(p.y>temph-5){
				setCursor(Cursor.getPredefinedCursor(Cursor.S_RESIZE_CURSOR));
			}else if(p.x<8 && p.y<8){
				setCursor(Cursor.getPredefinedCursor(Cursor.NW_RESIZE_CURSOR));
			}else if(p.x<5){
				setCursor(Cursor.getPredefinedCursor(Cursor.W_RESIZE_CURSOR));
			}else if(p.y<5){
				setCursor(Cursor.getPredefinedCursor(Cursor.N_RESIZE_CURSOR));
			}else if(p.x>tempw-8 && p.y<8){
				setCursor(Cursor.getPredefinedCursor(Cursor.NE_RESIZE_CURSOR));
			}else if(p.x<8 && p.y>temph-8){
				setCursor(Cursor.getPredefinedCursor(Cursor.SW_RESIZE_CURSOR));
			}else {
				if(p.x < tempw && p.y < 30)
				{
					setCursor( Cursor.getPredefinedCursor( Cursor.HAND_CURSOR)); 
				}else{
					setCursor( Cursor.getPredefinedCursor( Cursor.DEFAULT_CURSOR)); 
				}
				
			}
		}
		
	}
}

